//
//  HDRootAudioVC.m
//  HDWuDaDaoServiceCenter
//
//  Created by HDNiuKuiming on 2017/4/10.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HDRootAudioVC.h"
#import "HDBottomBarView.h"
#import "HDMuseumIntrodeceVC.h"
#import "HD_NKM_Mediator.h"
#import "HDMuseumListCell.h"

@interface HDRootAudioVC ()<HDBottomBarViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) NSDictionary *pushDic;

@end

@implementation HDRootAudioVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    [self barInit];
    _dataArray = nil;
    _pushDic = nil;
    
    [self tableviewInit];
    [self performSelector:@selector(startBeaconAction) withObject:nil afterDelay:3.0f];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self museumInfoRequestAction]; // 获取博物馆信息
}

- (void)startBeaconAction
{
    dispatch_async(dispatch_get_main_queue(), ^{
        HD_NKM_Mediator *beaconLoc = [HD_NKM_Mediator shareInstance];
        NSArray *vcArray = @[@"HDMapVC"];
        [beaconLoc initValideVCList:vcArray];
        [beaconLoc startRangingAllRegions];
    });
}

- (void)barInit
{
    HDBottomBarView *bar = [[[NSBundle mainBundle] loadNibNamed:@"HDBottomBarView" owner:self options:nil] lastObject];
    [_bottomView addSubview:bar];
    [bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_bottomView).with.offset(0);
        make.right.equalTo(_bottomView).with.offset(0);
        make.left.equalTo(_bottomView).with.offset(0);
        make.bottom.equalTo(_bottomView).with.offset(0);
    }];
    [bar hightedBtn:2];
    bar.delegate = self;
}

- (void)barSelectAction:(NSString *)VCStr
{
    NSArray *vcArray = self.navigationController.viewControllers;
    BOOL isVCExit = NO;
    for (UIViewController *vc in vcArray) {
        if ([vc isKindOfClass:NSClassFromString(VCStr)]) {
            isVCExit = YES;
            [self.navigationController popToViewController:vc animated:NO];
            break;
        }
    }
    
    if (isVCExit == NO) {
        UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        UIViewController *pushVC = [board instantiateViewControllerWithIdentifier:VCStr];
        [self.navigationController pushViewController:pushVC animated:NO];
    }
}

- (void)museumInfoRequestAction {
    
    HDRequestObj *loginObj = [[HDRequestObj alloc] init];
    NSString *urlPath = [NSString stringWithFormat:@"%@g=mapi&m=Contentinfo&a=all_museum", HTTP_Request_URL_Prefix];
    NSDictionary *parameters = @{@"user_login": [[HDDeclare sharedDeclare] userPhoneID],
                                 @"language": @"CHINESE"};
    
    [loginObj requestGetPath:urlPath Parameter:parameters withVC:self timeout:7];
    loginObj.myDataBlock = ^(NSDictionary *dataDic){
        
        NSString *loginCode = dataDic[@"status"];
        if (loginCode.integerValue == 1) {
            _dataArray = dataDic[@"data"];
            [_myTableView reloadData];
        }
        else {
        }
    };
}

#pragma mark
#pragma mark -------- Tableview Methods --------

- (void)tableviewInit {
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.rowHeight = 44*ScreenWidth/320;
    _myTableView.separatorColor = [UIColor clearColor];
    _myTableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rootvc_bg"]];
    _myTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"HDMuseumListCell";
    HDMuseumListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil)
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HDMuseumListCell" owner:self options:nil] lastObject];
    
    NSDictionary *nowDic = _dataArray[indexPath.row];
    cell.textLabel.text = nowDic[@"title"];
    cell.textLabel.font = [UIFont fontWithName:@"FangSong" size:15.0f];
    
    NSString *isValide = nowDic[@"is_valid"];
    if (isValide.integerValue == 0) {
        cell.earPhoneImgView.hidden = YES;
    }
    else {
        cell.earPhoneImgView.hidden = NO;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _pushDic = _dataArray[indexPath.row];
    [self performSegueWithIdentifier:@"museumListPushToIntroduceLine" sender:nil];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSString *segStr = segue.identifier;
    if ([segStr isEqualToString:@"museumListPushToIntroduceLine"]) {
        HDMuseumIntrodeceVC *VC = segue.destinationViewController;
        VC.requestDic = _pushDic;
    }
}

#pragma mark
#pragma mark -------- Dealloc Methods --------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
